﻿
#include "Scene0910.h"
#include "SimpleAudioEngine.h"
#include "Util.h"
#include "cocos\ui\CocosGUI.h"
#include "cocos\base\CCVector.h"
#include "Scene0917.h";

using namespace cocos2d::ui;

Scene* Scene0910::createScene()
{
	return Scene0910::create();
}

bool Scene0910::init()
{
	if (!Scene::init())
	{
		return false;
	}

	//场景背景
	auto background = Sprite::create("res/Background.jpg");
	background->setPosition(VISIBLE_SIZE / 2);
	background->setScale(VISIBLE_SIZE.width / background->getContentSize().width,
		VISIBLE_SIZE.height / background->getContentSize().height);
	addChild(background);

	//排行榜背景
	auto rankingBg = Sprite::create("res/bg.png");
	rankingBg->setPosition(VISIBLE_SIZE / 2);
	addChild(rankingBg);

	//表格背景
	tbBg = Sprite::create("res/tablebg.png");
	tbBg->setPosition(Vect(VISIBLE_SIZE.width / 2, VISIBLE_SIZE.height / 2 - 40));
	addChild(tbBg);

	//按钮
	auto ruleBtn = Button::create("res/normal.png");
	ruleBtn->setPosition(Vec2(rankingBg->getContentSize().width / 2 - 200, rankingBg->getContentSize().height - 130));
	rankingBg->addChild(ruleBtn);

	auto competitionBtn = Button::create("res/match.png");
	competitionBtn->setPosition(Vec2(rankingBg->getContentSize().width / 2 + 200, rankingBg->getContentSize().height - 130));
	rankingBg->addChild(competitionBtn);

	auto closeBtn = Button::create("res/close.png");
	closeBtn->setPosition(Vec2(rankingBg->getContentSize().width - 20, rankingBg->getContentSize().height - 20));
	rankingBg->addChild(closeBtn);

	//Label
	auto numberLable = Label::create("My ranking: ", "fonts/arialrounded.TTF", 25);
	numberLable->setPosition(Vec2(150, 60));
	rankingBg->addChild(numberLable);

	auto number = Label::create("5", "fonts/arialrounded.TTF", 25);
	number->setPosition(Vec2(240, 57));
	number->setColor(Color3B::RED);
	rankingBg->addChild(number);

	TableView* tableView = TableView::create(this, tbBg->getContentSize());
	tableView->setDelegate(this);
	tableView->setVerticalFillOrder(TableView::VerticalFillOrder::TOP_DOWN);//从上到下
	tbBg->addChild(tableView);

	//初始化数组 可变数组
	models = __Array::createWithCapacity(10);
	models->addObject(__String::create("房间3"));
	models->addObject(__String::create("房间11"));
	models->retain();//计数+1
	//当函数执行完成,系统检测当前的内存释放池，当计数为零时会被放入内存释放池
	//models->release();//计数-1

	tableView->reloadData();//刷新

	//// 给到默认头像
	//headImg = Sprite::create("res/net.png");
	//headImg->setPosition(VISIBLE_SIZE / 2);
	//addChild(headImg);

	//// 发送请求
	//createHttpOfHeadImg();

	return true;
}
//
//void Scene0910::createHttpOfHeadImg()
//{
//	log("========发送请求========");
//	// 生成httpRequest对象
//	auto request = new HttpRequest();
//	// 设置请求的地址
//	request->setUrl("http://www.51kellen.xin:8080/images/head_1.jpg");
//	// 设置请求方式
//	request->setRequestType(HttpRequest::Type::GET);
//	// 设置请求的回调函数
//	request->setResponseCallback(CC_CALLBACK_2(Scene0910::getHeadImgResponse, this));
//	// 发送请求
//	HttpClient::getInstance()->send(request);
//	// 释放HttpRequest对象
//	request->release();
//	log("========请求结束========");
//}
//
//void Scene0910::getHeadImgResponse(HttpClient * sender, HttpResponse * response)
//{
//	// 获取响应数据
//    vector<char>* buffer =	response->getResponseData();
//	// 创建图片
//	Image* img = new Image();
//	bool isSuccess = img->initWithImageData((unsigned char*) buffer->data(), buffer->size());
//	if (!isSuccess)
//		return;
//	// 创建纹理
//	Texture2D* texture = new Texture2D();
//	bool isImg = texture->initWithImage(img);
//	if (isImg) {
//		headImg->setTexture(texture);
//	}
//}

void Scene0910::tableCellTouched(TableView* table, TableViewCell* cell) {
	//log("Cell %lu ", cell->getIdx());
	Scene0917* scene1 = (Scene0917*)Scene0917::createScene();
	scene1->setRoomId(cell->getIdx() + 1);
	//TransitionProgress::create()
	Director::getInstance()->replaceScene( scene1);
}

TableViewCell* Scene0910::tableCellAtIndex(TableView *table, ssize_t idx) {
	TableViewCell* cell = table->dequeueCell();

	if (!cell) {
		cell = TableViewCell::create();
	}
	cell->removeAllChildrenWithCleanup(true);
	auto cellSprite = Sprite::create("res/cell.png");
	cellSprite->setPositionX(tbBg->getContentSize().width / 2);
	cell->addChild(cellSprite);

	auto numberLabel = Label::create(StringUtils::format( "%s",datas[idx][0].getCString()), "fonts/arialrounded.TTF",30);
	numberLabel->setColor(Color3B::RED);
	numberLabel->setPosition(120, 25);
	cellSprite->addChild(numberLabel);

	auto icon = Sprite::create(datas[idx][1].getCString());
	icon->setScale(0.08f);
	icon->setPosition(240, 25);
	cellSprite->addChild(icon);

	auto nameLabel = Label::create(StringUtils::format("%s", datas[idx][2].getCString()), "fonts/arialrounded.TTF", 30);
	nameLabel->setColor(Color3B::RED);
	nameLabel->setPosition(400, 25);
	cellSprite->addChild(nameLabel);

	auto scoreLabel = Label::create(StringUtils::format("%s", datas[idx][3].getCString()), "fonts/arialrounded.TTF", 30);
	scoreLabel->setColor(Color3B::WHITE);
	scoreLabel->setPosition(750, 25);
	cellSprite->addChild(scoreLabel);

	return cell;
}

Size Scene0910::tableCellSizeForIndex(TableView * table, ssize_t idx)
{
	return Size(tbBg->getContentSize().width, 58);
}

ssize_t Scene0910::numberOfCellsInTableView(TableView * table)
{
	return sizeof(datas) / sizeof(datas[0]);
}
